<script setup>
import { ref,reactive,unref } from 'vue'
import useBannersStore from '../../store/bannersStore'
import { ElMessageBox,ElMessage } from 'element-plus'

const bannersStore = useBannersStore()

const editId = ref('')
const dialogImageUrl = ref('')
const imgDialogVisible = ref(false)
const fileList = reactive({
  list: []
})
const bannerForm = reactive({
  title: "",
  img: "",
  status: 1
})
const rules = reactive({
  title: {
    required: true,
    message: "请输入图片名称",
    trigger: "blur",
  },
  img: { required: true, message: "请选择封面图片", trigger: "blur" },
  status: { required: true, message: "请选择图片状态", trigger: "blur" }
})
// 导出父组件需用到属性与方法
defineExpose({
  editId,bannerForm,fileList
})

// 获取组件el-form上的ref
const formRef = ref(null)

// 点击文件列表中已上传的文件
function handlePictureCardPreview(file) {
  // console.log(file,'点击文件')
  dialogImageUrl.value = file.url
  imgDialogVisible.value = true
}
// 文件状态改变时
function changeFile(file) {
  // console.log(file,'文件状态改变了')
  // 设置表单封面
  bannerForm.img = file.raw
}
// 超出限制
function limitTips() {
  ElMessage({
    type: 'warning',
    message: '分类封面图片最多只允许上传一张'
  })
}

// 重置表单
const resetForm = () => {
  editId.value = ''
  fileList.list = []
  bannerForm.title = ''
  bannerForm.img = ''
  bannerForm.status = 1
  // 关闭对话框
  bannersStore.closeDialog()
}
// 提交表单
const submitForm = () => {
  // console.log(bannerForm,'bannerForm------')
  let form = unref(formRef)
  form.validate((valid) => {
    if (valid) {
      // console.log('submit!')
      if(editId.value) {
        // 编辑
        // console.log(editId,'编辑-----')
        bannersStore.editBannersAction({...bannerForm,id: editId.value})
      } else {
        // console.log(bannerForm,'新增----')
        bannersStore.addBannersAction(bannerForm)
      }
      // 重置表单
      resetForm()
    } else {
      // console.log('error submit!')
      return false
    }
  })
}

const handleClose = (done) => {
  ElMessageBox.confirm('你确定要关闭吗？')
    .then(() => {
      done()
      // 重置表单
      resetForm()
    })
    .catch(() => {
      // catch error
    })
}
</script>

<template>
  <!-- 对话框 -->
  <el-dialog
    :title="editId ? '编辑轮播图' : '新增轮播图'"
    v-model="bannersStore.dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      :model="bannerForm"
      :rules="rules"
      ref="formRef"
      label-width="100px"
    >
      <el-form-item label="图片名称" prop="title">
        <el-input v-model="bannerForm.title" placeholder="请输入图片名称" />
      </el-form-item>
      <el-form-item label="封面图片" prop="img">
        <el-upload
          action="#"
          :file-list="fileList.list"
          list-type="picture-card"
          :auto-upload="false"
          :on-preview="handlePictureCardPreview"
          :on-change="changeFile"
          :limit="1"
          :on-exceed="limitTips"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
        <el-dialog v-model="imgDialogVisible">
          <img w-full :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="bannerForm.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="submitForm">{{ editId? '编辑轮播图':'新增轮播图' }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
